package com.sysu.simulation.widget;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;

import com.sysu.simulation.BaseFragment;
import com.sysu.simulation.InfoFragment;
import com.sysu.simulation.SimFragment;
import com.sysu.simulation.RecordFragment;
import com.sysu.simulation.R;

public class BottomNavFragment extends BaseFragment implements View.OnClickListener{
    private Context mContext;
    private int mContainerId;
    private FragmentManager mFragmentManager;
    private BottomNavButton mCurrentNavButton;
    private OnNavigationReselectListener mOnNavigationReselectListener;

    private BottomNavButton mNavRecords;
    private BottomNavButton mNavSim;
    private BottomNavButton mNavInfo;

    public BottomNavFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);

        mNavRecords = (BottomNavButton) findView(R.id.nav_records);
        mNavSim = (BottomNavButton) findView(R.id.nav_sim);
        mNavInfo = (BottomNavButton) findView(R.id.nav_info);

        mNavRecords.init(R.drawable.bottom_nav_icon_records,
                R.string.main_nav_records,
                RecordFragment.class);

        mNavSim.init(R.drawable.bottom_nav_icon_sim,
                R.string.main_nav_sim,
                SimFragment.class);

        mNavInfo.init(R.drawable.bottom_nav_icon_info,
                R.string.main_nav_info,
                InfoFragment.class);

        mNavRecords.setOnClickListener(this);
        mNavSim.setOnClickListener(this);
        mNavInfo.setOnClickListener(this);
    }

    public void setup(Context context, FragmentManager fragmentManager, int contentId, OnNavigationReselectListener listener) {
        mContext = context;
        mFragmentManager = fragmentManager;
        mContainerId = contentId;
        mOnNavigationReselectListener = listener;

        doSelect(mNavSim, null);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_bottom_nav;
    }

    @Override
    public void onClick(View v) {
        if (v instanceof BottomNavButton) {
            BottomNavButton nav = (BottomNavButton) v;
            doSelect(nav, null);
        }
    }

    public void doSelect(int index, Bundle extra) {
        switch(index) {
            case 0:
                doSelect(mNavRecords, extra);
                break;
            case 1:
                doSelect(mNavSim, extra);
                break;
            case 2:
                doSelect(mNavInfo, extra);
                break;
            default:
                break;
        }
    }

    private void doSelect(BottomNavButton newNavButton, Bundle extra) {
        BottomNavButton oldNavButton = null;
        if (mCurrentNavButton != null) {
            oldNavButton = mCurrentNavButton;
            if (oldNavButton == newNavButton) {
                onReselect(oldNavButton);
                return;
            }
            oldNavButton.setSelected(false);
        }
        newNavButton.setSelected(true);
        doTabChanged(oldNavButton, newNavButton, extra);
        mCurrentNavButton = newNavButton;
    }

    private void doTabChanged(BottomNavButton oldNavButton, BottomNavButton newNavButton, Bundle extra) {
        FragmentTransaction ft = mFragmentManager.beginTransaction();
        if (oldNavButton != null) {
            if (oldNavButton.getFragment() != null) {
                ft.detach(oldNavButton.getFragment());
            }
        }
        if (newNavButton != null) {
            if (newNavButton.getFragment() == null) {
                Fragment fragment = Fragment.instantiate(mContext,
                        newNavButton.getClx().getName(), null);
                if(extra!=null) ((BaseFragment)fragment).setExtra(extra);
                ft.add(mContainerId, fragment, newNavButton.getTag());
                newNavButton.setFragment(fragment);
            } else {
                if(extra!=null) ((BaseFragment)newNavButton.getFragment()).setExtra(extra);
                ft.attach(newNavButton.getFragment());
            }
        }
        ft.commit();
    }

    private void onReselect(BottomNavButton navButton) {
        OnNavigationReselectListener listener = mOnNavigationReselectListener;
        if (listener != null) {
            listener.onReselect(navButton);
        }
    }

    public interface OnNavigationReselectListener {
        void onReselect(BottomNavButton navButton);
    }
}
